﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WeChat.Common2;
using System.Web.Script.Serialization;
using DAL;
//ashx为快速更新接口，类文件为管理系统调用接口
namespace WeChat.Common
{
    public class SetMenu
    {
        public static string setMenu()
        {
            //menuJson为菜单json
            //string menuJson = EditStrings.menuJson;

            string menuJson = setMenuJson();
            
            //获取access_token
            string accesstoken = GetAccess_token.getAccesstoken();
            string url = " https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accesstoken;
            GetReturnJson gt = new GetReturnJson();
            //Dictionary<string, object> da = gt.postReturnJson(url, menuJson);
            var js = new JavaScriptSerializer();
            var da = js.Deserialize<Dictionary<string, object>>(gt.postReturnJson(url, menuJson));
            if (da["errcode"].ToString().Equals("0"))
            {
                ApplicationLog.Write("菜单请求成功！");
                return "菜单请求成功！";
            }
            else
            {
                ApplicationLog.Write("菜单请求失败！errcode：" + da["errcode"]);
                return "菜单请求失败！errcode：" + da["errcode"];
            }
        }
        public static string setOrderMenu()
        {
            //menuJson为菜单json
            //string menuJson = EditStrings.menuJson;

            string menuJson = setOrderMenuJson();

            //获取access_token
            string accesstoken = GetAccess_token.getAccesstoken();
            string url = " https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accesstoken;
            GetReturnJson gt = new GetReturnJson();
            //Dictionary<string, object> da = gt.postReturnJson(url, menuJson);
            var js = new JavaScriptSerializer();
            var da = js.Deserialize<Dictionary<string, object>>(gt.postReturnJson(url, menuJson));
            if (da["errcode"].ToString().Equals("0"))
            {
                ApplicationLog.Write("菜单请求成功！");
                return "菜单请求成功！";
            }
            else
            {
                ApplicationLog.Write("菜单请求失败！errcode：" + da["errcode"]);
                return "菜单请求失败！errcode：" + da["errcode"];
            }
        }
        //public static string menuJson = "{\"button\":[{\"name\":\"公司资讯\",\"sub_button\":[{\"type\":\"click\",\"name\":\"公司简介\",\"key\":\"Introductions\"},"
        //                   + " {\"type\":\"click\",\"name\":\"最新资讯\",\"key\":\"Information_pushed\"},{\"type\":\"click\",\"name\":\"连接客服\",\"key\":\"Customer_service\"}]}, {\"name\":\"查看信息\",\"sub_button\":[{\"type\":\"click\",\"name\":\"个人信息\",\"key\":\"Personal_Info\"},"
        //                   + " {\"type\":\"click\",\"name\":\"充值记录\",\"key\":\"Recharge_Records\"},{\"type\":\"click\",\"name\":\"消费记录\",\"key\":\"Consumption_Records\"}]},{\"type\":\"click\",\"name\":\"余额充值\",\"key\":\"Charge_Records\"}]}";

        //生成菜单json
        public static string setMenuJson()
        {
            JObject jo = new JObject();
            JArray ja = new JArray();
            jo.Add("button",ja);

	    //       //菜单1
	    //       JObject menu1 = new JObject();
	    //       JArray j1 = new JArray();
	    //       menu1.Add("name","公司资讯");//一级菜单名
	    //       menu1.Add("sub_button", j1);//二级菜单
	    //       j1.Add(setClickM("view", "公司官网", "http://www.jocat.com"));
	    //j1.Add(setClickM("click", "公司简介", "Introductions"));

	    //       j1.Add(setClickM("click", "最新资讯", "Information_pushed"));
	    //       j1.Add(setClickM("click", "连接客服", "Customer_service"));

	    //       //菜单2
	    //       JObject menu2 = new JObject();
	    //       JArray j2 = new JArray();
	    //       menu2.Add("name", "查看信息");//一级菜单名
	    //       menu2.Add("sub_button", j2);//二级菜单
	    //       j2.Add(setClickM("view", "个人信息", OAuthHelper.getRedirectUrl("http://"+Config.domainName+"/WeChat/UserInfo/PersonalInfo.aspx")));
	    //       j2.Add(setClickM("view", "充值记录", OAuthHelper.getRedirectUrl("http://" + Config.domainName + "/WeChat/UserInfo/RechargeRecords.aspx")));
	    //       j2.Add(setClickM("view", "消费记录", OAuthHelper.getRedirectUrl("http://" + Config.domainName + "/WeChat/UserInfo/ConsumptionRecords.aspx")));
	    //       //菜单3
	    //       JObject menu3 = setClickM("view", "余额充值", OAuthHelper.getRedirectUrl("http://" + Config.domainName + "/WeChat/JsSDK/WxPay/WechatPay.aspx"));
	    JObject menu3 = setClickM("view", "消费管理系统", "http://498.jocat.com/Binding.aspx");
	    //       ja.Add(menu1);
	    //       ja.Add(menu2);
	    ja.Add(menu3);
            return jo.ToString();
        }
        public static string setOrderMenuJson()
        {
            JObject jo = new JObject();
            JArray ja = new JArray();
            jo.Add("button", ja);

	    //       //菜单1
	    //       JObject menu1 = new JObject();
	    //       JArray j1 = new JArray();
	    //       menu1.Add("name", "公司资讯");//一级菜单名
	    //       menu1.Add("sub_button", j1);//二级菜单
	    //       j1.Add(setClickM("view", "公司官网", "http://www.jocat.com"));
	    //j1.Add(setClickM("click", "公司简介", "Introductions"));

	    //       j1.Add(setClickM("click", "最新资讯", "Information_pushed"));
	    //       j1.Add(setClickM("click", "连接客服", "Customer_service"));

	    ////菜单2
	    //JObject menu2 = new JObject();
	    //JArray j2 = new JArray();
	    //menu2.Add("name", "个人中心");//一级菜单名
	    //menu2.Add("sub_button", j2);//二级菜单
	    //j2.Add(setClickM("view", "个人信息", OAuthHelper.getRedirectUrl("http://" + Config.domainName + "/WeChat/UserInfo/PersonalInfo.aspx")));
	    //j2.Add(setClickM("view", "充值记录", OAuthHelper.getRedirectUrl("http://" + Config.domainName + "/WeChat/UserInfo/RechargeRecords.aspx")));
	    //j2.Add(setClickM("view", "消费记录", OAuthHelper.getRedirectUrl("http://" + Config.domainName + "/WeChat/UserInfo/ConsumptionRecords.aspx")));
	    //j2.Add(setClickM("view", "余额充值", OAuthHelper.getRedirectUrl("http://" + Config.domainName + "/WeChat/JsSDK/WxPay/WechatPay.aspx")));
	    //j2.Add(setClickM("view", "我的二维码", OAuthHelper.getRedirectUrl("http://" + Config.domainName + "/WeChat/UserInfo/Qrcode.aspx")));

	    //菜单3
	    //JObject menu3 = setClickM("view", "在线订餐", OAuthHelper.getRedirectUrl("http://" + Config.domainName + "/WeChat/MenuReservation/ReserveMenu.aspx"));
	    JObject menu3 = setClickM("view", "消费管理系统", "http://498.jocat.com/Binding.aspx");
	    //ja.Add(menu1);
	    //       ja.Add(menu2);
	    ja.Add(menu3);
            return jo.ToString();
        }
        //click菜单json
        public static JObject setClickM(string type, string name, string key)
        {
            JObject jo = new JObject();
            jo.Add("type", type);
            jo.Add("name",name);
            string m = "";
            if (type.Equals("click"))
            {
                m = "key";
            }
            else if (type.Equals("view"))
            {
                m = "url";
            }
            jo.Add(m,key);
            return jo;
        }
    }


}
